package org.eredlab.g4.lz.web;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.eredlab.g4.arm.util.idgenerator.IDHelper;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.lz.service.GoodService;
import org.eredlab.g4.lz.util.BaseUtil;
import org.eredlab.g4.rif.util.SessionContainer;
import org.eredlab.g4.rif.util.WebUtils;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

public class BaseDataAction extends BaseAction {
	
	/**
	 * service
	 */
	private GoodService goodservice = (GoodService)getService("GoodService");
	
	
	/**
	 * 商品初始查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goodInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("goodDemoInit");
	}
	/**
	 * 保存新建商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		//生产时间当前时间
		inDto.put("createTime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
		inDto.put("operName", sessionContainer.getUserInfo().getUserid());//操作人姓名
		String type = inDto.getAsString("category");
		//保存数据库
		String re = "TY";//BaseUtil.getGoodCategory(type);
		if("".equalsIgnoreCase(re)){
			setOkTipMsg("产品数据类型错误", response);
			super.write(inDto.toJson(), response);
			return mapping.findForward(null);
		}else{
			inDto.put("serialNo", IDHelper.getGoodId(re, 0));//ID
			goodservice.insertGood(inDto);
			setOkTipMsg("数据保存成功", response);
			super.write(inDto.toJson(), response);
			return mapping.findForward(null);
		}
		
	}
	
	/**
	 * 修改商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm cForm = (CommonActionForm) form;
		Dto inDto = cForm.getParamAsDto(request);
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		inDto.put("operName", sessionContainer.getUserInfo().getUserid());//操作人姓名
		goodservice.updateGood(inDto);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}
	
	/**
	 * 删除商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		goodservice.deleteGood(inDto);
		setOkTipMsg("删除成功", response);
		return mapping.findForward(null);
	}
	
	/**
	 * 查询商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.good_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("lz.good_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/**
	 * 插入图片
	 * 
	 * @param
	 * @return
	 */
	public ActionForward doUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Dto outDto = new BaseDto();
		CommonActionForm cForm = (CommonActionForm) form;
		// 单个文件,如果是多个就cForm.getFile2()....支持最多5个文件
		FormFile myFile = cForm.getFile1();
		// 获取web应用根路径,也可以直接指定服务器任意盘符路径
		String savePath = getServlet().getServletContext().getRealPath("/") + "uploaddata/demo/";
		//String savePath = "d:/upload/";
		// 检查路径是否存在,如果不存在则创建之
		File file = new File(savePath);
		if (!file.exists()) {
			file.mkdir();
		}
		String type = myFile.getFileName().substring(myFile.getFileName().lastIndexOf("."));
		// 文件真实文件名
		String fileName = getSessionContainer(request).getUserInfo().getUserid();
		fileName = fileName + "_" + G4Utils.getCurrentTime("yyyyMMddhhmmss") + type;
		// 我们一般会根据某种命名规则对其进行重命名
		// String fileName = ;
		File fileToCreate = new File(savePath, fileName);
		if (myFile.getFileSize() > 204800) {
			outDto.put("success", new Boolean(true));
			outDto.put("msg", "文件上传失败,你只能上传小于100KB的图片文件");
			outDto.put("state", "error");
		}else {
			// 检查同名文件是否存在,不存在则将文件流写入文件磁盘系统
			if (!fileToCreate.exists()) {
				FileOutputStream os = new FileOutputStream(fileToCreate);
				os.write(myFile.getFileData());
				os.flush();
				os.close();
			}
			outDto.put("success", new Boolean(true));
			outDto.put("msg", "文件上传成功");
			outDto.put("state", "ok");
			outDto.put("aUrl", request.getContextPath() + "/uploaddata/demo/" + fileName);
		}
		write(outDto.toJson(), response);
		return mapping.findForward(null);
	}
	
	/**
	 * 商品初始查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward experInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("experInit");
	}
	
	/**
	 * 查询商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryExper(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.exper_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("lz.exper_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	
	/**
	 * 保存新建商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveExper(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		//生产时间当前时间
		inDto.put("createtime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
		inDto.put("opetper", sessionContainer.getUserInfo().getUserid());//操作人姓名
		String type = inDto.getAsString("category");
		//保存数据库
		String re = "SY";//BaseUtil.getGoodCategory(type);
		if("".equalsIgnoreCase(re)){
			setOkTipMsg("产品数据类型错误", response);
			super.write(inDto.toJson(), response);
			return mapping.findForward(null);
		}else{
			inDto.put("expid", IDHelper.getGoodId(re, 0));//ID
			goodservice.insertExper(inDto);
			setOkTipMsg("数据保存成功", response);
			super.write(inDto.toJson(), response);
			return mapping.findForward(null);
		}
	}
	
	/**
	 * 修改商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateExper(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm cForm = (CommonActionForm) form;
		Dto inDto = cForm.getParamAsDto(request);
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		inDto.put("createtime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
		inDto.put("opetper", sessionContainer.getUserInfo().getUserid());//操作人姓名
		goodservice.updateExper(inDto);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}
	
	/**
	 * 删除商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteExper(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		goodservice.deleteExper(inDto);
		setOkTipMsg("删除成功", response);
		return mapping.findForward(null);
	}
}
